package hp.com.dao.impl;

import hp.com.dao.BaseDao;
import hp.com.dao.UsersDAO;
import hp.com.entity.Users;

import java.sql.ResultSet;

public class UsersDAOImpl extends BaseDao implements UsersDAO {

    //用户注册添加方法
    @Override
    public int addUsers(Users users) throws Exception {
        String sql = "insert into users value(?,?,?,?,?,?,?)";
        Object[] obj = new Object[7];
        obj[0] = null;
        obj[1] = users.getLoginname();
        obj[2] = users.getPwd();
        obj[3] = users.getEmail();
        obj[4] = users.getPhone();
        obj[5] = 1;
        obj[6] = "/jbs/images/defaultProfilePic.png";
        return executeUpdate(sql,obj);
    }

    // 用户登录
    @Override
    public boolean UsersById(Users users) throws Exception {
        String sql = "select * from users where phone = ? and pwd = ?";
        Object[] obj = new Object[2];
        obj[0] = users.getPhone();
        obj[1] = users.getPwd();
        ResultSet rs = executeQuery(sql,obj);
        return rs.next();
    }

    // 根据用户手机号进行查询
    @Override
    public Users checkPhone(Users users) throws Exception {
        String sql = "select * from users where phone = ?";
        Object[] obj = new  Object[1];
        obj[0] =users.getPhone();
        ResultSet rs = executeQuery(sql, obj);
        if (rs.next()){
            Users user = new Users();
            user.setId(rs.getInt("id"));
            user.setEmail(rs.getString("email"));
            user.setUsertype(rs.getInt("usertype"));
            user.setLoginname(rs.getString("loginname"));
            user.setPhone(rs.getString("phone"));
            user.setPic_link(rs.getString("pic_link"));
            switch (user.getUsertype()){
                case 1:
                    user.setUserTypeText("青鸟会员");
                    break;
                case 2:
                    user.setUserTypeText("金凰会员");
                    break;
                case 3:
                    user.setUserTypeText("黑鹰会员");
                    break;
            }
            return user;
        }
        return null;
    }

    // 验证用户修改时手机号是否输入正确
    @Override
    public boolean checkOldPhone(Users users) throws Exception {
        String sql = "select * from users where id = ? and phone = ?";
        Object[] obj = new Object[2];
        obj[0] = users.getId();
        obj[1] = users.getPhone();
        ResultSet rs = executeQuery(sql, obj);
        return rs.next();
    }

    // 验证手机号是否被其它用户注册
    @Override
    public boolean checkNewPhone(Users users) throws Exception {
        String sql = "select * from users where id != ? and phone = ?";
        Object[] obj = new Object[2];
        obj[0] = users.getId();
        obj[1] = users.getPhone();
        ResultSet rs = executeQuery(sql, obj);
        return rs.next();
    }

    // 用户修改手机号
    @Override
    public int updateUserPhone(Users users) throws Exception {
        String sql = "update users set phone = ? where id = ?";
        Object[] obj = new Object[2];
        obj[0] = users.getPhone();
        obj[1] = users.getId();
        return executeUpdate(sql, obj);
    }

    // 验证用户修改时邮箱是否输入正确
    @Override
    public boolean checkOldEmail(Users users) throws Exception {
        String sql = "select * from users where id = ? and email = ?";
        Object[] obj = new Object[2];
        obj[0] = users.getId();
        obj[1] = users.getEmail();
        ResultSet rs = executeQuery(sql, obj);
        return rs.next();
    }

    // 用户修改邮箱
    @Override
    public int updateUserEmail(Users users) throws Exception {
        String sql = "update users set email = ? where id = ?";
        Object[] obj = new Object[2];
        obj[0] = users.getEmail();
        obj[1] = users.getId();
        return executeUpdate(sql, obj);
    }

    // 验证用户修改时原密码是否输入正确
    @Override
    public boolean checkOldPwd(Users users) throws Exception {
        String sql = "select * from users where id = ? and pwd = ?";
        Object[] obj = new Object[2];
        obj[0] = users.getId();
        obj[1] = users.getPwd();
        ResultSet rs = executeQuery(sql, obj);
        return rs.next();
    }

    @Override
    public Users getUserByID(int ID) throws Exception {
        String sql = "select * from users where id=?";
        ResultSet rs =this.executeQuery(sql,new Object[]{ID});
        if(rs.next()){
            Users user = new Users();
            user.setId(ID);
            user.setEmail(rs.getString("email"));
            user.setUsertype(rs.getInt("usertype"));
            user.setLoginname(rs.getString("loginname"));
            user.setPhone(rs.getString("phone"));
            user.setPic_link(rs.getString("pic_link"));
            switch (user.getUsertype()){
                case 1:
                    user.setUserTypeText("青鸟会员");
                    break;
                case 2:
                    user.setUserTypeText("金凰会员");
                    break;
                case 3:
                    user.setUserTypeText("黑鹰会员");
                    break;
            }
            return user;
        }
        return null;
    }

    // 通过id进行密码修改
    @Override
    public int updateUsersPwd(Users users) throws Exception {
        String sql = "update users set pwd = ? where id = ?";
        Object[] obj = new Object[2];
        obj[0] = users.getPwd();
        obj[1] = users.getId();
        return executeUpdate(sql,obj);
    }

    @Override
    public boolean checkPhoneValid(String phone) throws Exception {
            String sql = "select * from users where phone = ?";
            ResultSet rs = executeQuery(sql, new Object[]{phone});
            if(rs.next()){
                return false;
        }
            return true;
    }
}
